package com.adams.app.ui;

import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshAttacher;
import android.app.ActionBar;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.widget.Toast;

import com.adams.app.R;
import com.adams.app.ui.fragment.NavigationDrawerFragment;

public class MainActivity extends ActionBarActivity implements
		NavigationDrawerFragment.NavigationDrawerCallbacks {

	private NavigationDrawerFragment mNavigationDrawerFragment;
	private PullToRefreshAttacher mPullToRefreshAttacher;
	private long exitTime = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		initMenu();
		this.findViews();

		mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
				(DrawerLayout) findViewById(R.id.drawer_layout));

		PullToRefreshAttacher.Options options = new PullToRefreshAttacher.Options();
		options.headerInAnimation = R.anim.pulldown_fade_in;
		options.headerOutAnimation = R.anim.pulldown_fade_out;
		options.refreshScrollDistance = 0.3f;
		options.headerLayout = R.layout.pulldown_header;
		mPullToRefreshAttacher = new PullToRefreshAttacher(this, options);
	}

	private void initMenu() {
		ActionBar actionBar = getActionBar();
		
		actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top));
	
		actionBar.show();
	}

	private void findViews() {
		mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
				.findFragmentById(R.id.navigation_drawer);
	}

	@Override
	public void onNavigationDrawerItemSelected(Uri uri) {
		updateView(uri);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK
				&& event.getAction() == KeyEvent.ACTION_DOWN) {

			if ((System.currentTimeMillis() - exitTime) > 2000) {
				Toast.makeText(getApplicationContext(), "再按一次退出程序",
						Toast.LENGTH_SHORT).show();
				exitTime = System.currentTimeMillis();
			} else {
				finish();
				System.exit(0);
			}
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	public void updateView(Uri uri) {

		Fragment fragment = null;
		final FragmentManager fm = getSupportFragmentManager();
		final FragmentTransaction tr = fm.beginTransaction();
		final String scheme = uri.getScheme();
		final String host = uri.getHost();

		if (scheme.equals("fragment")) {
			try {
				Class<?> clazz = Class.forName("com.adams.app.ui.fragment."
						+ host);
				fragment = (Fragment) clazz.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}
		if (fragment.isAdded()) {
			tr.show(fragment);
		} else {
			tr.replace(R.id.container, fragment, null);
		}
		tr.commit();
	}

	public PullToRefreshAttacher getPullToRefreshAttacher() {
		return mPullToRefreshAttacher;
	}
}
